package net.kunyv.base.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.kunyv.base.entity.SysCode;
import net.kunyv.base.mapper.SysCodeMapper;
import net.kunyv.util.PageResult;
import net.kunyv.util.PageParam;
import net.kunyv.util.SortParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 代码表Service
 *
 * @author sunyb
 * @date 2017-08-02
 */
@Service
public class SysCodeService extends BaseService<SysCode> {
    @Autowired
    private SysCodeMapper sysCodeMapper;

    /**
     * 获取代码表维护datagrid数据
     *
     * @param fldEng
     * @param code
     * @param pageParam
     * @return net.kunyv.bde.ssm.PageResult
     * @throw
     * @author sunyb
     * @date 2017/8/3
     */
    public PageResult getSysCodeGridData(String fldEng, String code, PageParam pageParam, SortParam sortParam) {
        Example example = new Example(SysCode.class);
        //设置排序
        example.setOrderByClause(sortParam.getSort() + " " + sortParam.getOrder());
        //拼接sql
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(fldEng)) {
            criteria.andEqualTo("fldEng", fldEng);
        }
        if (StringUtils.isNotEmpty(code)) {
            criteria.andLike("code", "%" + code.toUpperCase() + "%");
        }
        //分页
        Page page = PageHelper.startPage(pageParam.getPage(), pageParam.getRows());
        List<SysCode> sysCodeList = sysCodeMapper.selectByExample(example);
        PageResult pageResult = new PageResult();
        pageResult.setRows(sysCodeList);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 获取代码项combobox下拉数据
     *
     * @param q
     * @return java.util.List<java.lang.String>
     * @throw
     * @author sunyb
     * @date 2017/8/3
     */
    public List<HashMap> getFldChiList(String q) {
        return sysCodeMapper.getFldChiList(q);
    }

    /**
     * 获获取业务表
     *
     * @return java.util.List<java.lang.String>
     * @throw
     * @author sunyb
     * @date 2017/8/3
     */
    public List<HashMap> getUserTableInfos() {
        return sysCodeMapper.getUserTableInfos();
    }

    /**
     * 获获取业务表字段
     *
     * @return java.util.List<java.lang.String>
     * @throw
     * @author sunyb
     * @date 2017/8/3
     */
    public List<HashMap> getUserTableFieldsInfos() {
        return sysCodeMapper.getUserTableFieldsInfos();
    }

    /**
     * 获取代码项combobox下拉数据
     *
     * @param q
     * @return java.util.List<java.lang.String>
     * @throw
     * @author sunyb
     * @date 2017/8/3
     */
    public List<HashMap> getUserTableFieldsInfo(String q) {
        return sysCodeMapper.getUserTableFieldsInfo(q);
    }


    /**
     * 新增或编辑
     *
     * @param sysCode
     * @param userId
     * @return java.lang.String
     * @throw
     * @author sunyb
     * @date 2017/8/7
     */
    public String save(SysCode sysCode, String userId) {
        Date now = new Date();
        sysCode.setUpdateTime(now);
        sysCode.setUpdateUser(userId);
        if (StringUtils.isEmpty(sysCode.getId())) {
            sysCode.setId(null);
            Example example = new Example(SysCode.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("fldEng", sysCode.getFldEng());
            criteria.andEqualTo("code", sysCode.getCode());
            List<SysCode> list = sysCodeMapper.selectByExample(example);
            if (list.size() > 0) {
                return "该类型代码下CODE已存在";
            } else {
                sysCode.setCreateTime(now);
                sysCode.setCreateUser(userId);
                sysCode.setId(null);
                int i = sysCodeMapper.insert(sysCode);
                if (i == 1) {
                    return "ok";
                } else {
                    return "新增失败";
                }
            }
        } else {
            Example example = new Example(SysCode.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("fldEng", sysCode.getFldEng());
            criteria.andEqualTo("code", sysCode.getCode());
            criteria.andNotEqualTo("id", sysCode.getId());
            List<SysCode> list = sysCodeMapper.selectByExample(example);
            if (list.size() > 0) {
                return "该类型代码下CODE已存在";
            } else {
                int i = sysCodeMapper.updateByPrimaryKeySelective(sysCode);
                if (i == 1) {
                    return "ok";
                } else {
                    return "更新失败";
                }
            }
        }
    }
}
